# -*- coding: utf-8 -*-
from __future__ import unicode_literals

# 用于加密和解密的 base62 字符串
wl_key = 'AfnEcWa82s4GwqHKuxe7SDm0tXk1639RITFQPdlrLYCvUMzN5gOZpjoyVihJbB'


# base62加密函数
def wlencode(num, addcrc=None):
    if isinstance(num, int) and num > 0:
        ret = ''
        crc = wl_key[int(str(num)[-1]) * 6]
        while num != 0:
            ret = (wl_key[num % 62]) + ret
            num = int(num / 62)
        if addcrc:
            # 在末尾添加一位随机字符
            ret += crc
        return ret


# base62解密函数
def wldecode(st, addcrc=None):
    if isinstance(st, str) or isinstance(st, unicode):
        ret, mult = 0, 1
        if addcrc:
            # 去掉最末一位的随机字符
            st = st[:-1]
        for c in reversed(st):
            ret += mult * wl_key.index(c)
            mult *= 62
        return ret
